﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.Charting;
using System.Globalization;
using DIST.LR.ZHJG.Events;


namespace DIST.LR.ZHJG.Controls.Chart
{
    public partial class ucMultiYChart : UserControl
    {
        
        public ucMultiYChart()
        {
            InitializeComponent();

            //订阅barChartBtn内部按钮切换
            EventAggregatorRepository.EventAggregator
              .GetEvent<chartBtnChangeEvent>()
              .Subscribe(TransitbarChartVisible);

        }
        public void TransitbarChartVisible(string btnName)
        {
            //object oo = CommonDefines.radioBtn.RadioBtn1;
            if (btnName == CommonDefines.barChartBtn|| btnName == CommonDefines.barChartBtn1)
            {
                BarChartData();
            }
            else if (btnName == CommonDefines.barChartBtn2)
            {
                BarChartData2();
            }
           
        }
        /// <summary>
        /// 注意事项要求AxisName与XAML中一一对应
        /// barchart
        /// </summary>
        public void BarChartData()
        {
            List<gdData> data = new List<gdData>();
            string[] months = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames;
            data.Add(new gdData(181, 50, "城区", "2012"));
            data.Add(new gdData(211, 60, "萧山区", "2012"));
            data.Add(new gdData(412, 62, "余杭区", "2012"));
            data.Add(new gdData(313, 45, "临安市", "2012"));
            data.Add(new gdData(463, 40, "桐庐县", "2012"));
            data.Add(new gdData(475, 69, "淳安县", "2012"));
            data.Add(new gdData(263, 39, "建德市", "2012"));
            data.Add(new gdData(351, 43, "富阳市", "2012"));
  

            MultiYChartValue myvalue1 = new MultiYChartValue();
            myvalue1.LegendLableName = "供地总量";
            myvalue1.AxisName = "Frist";//必须与XAML中对应
            myvalue1.YValueName = "Gdzl";
            myvalue1.XCategoryName = "Xzqmc";
            //myvalue1.Color = this.Resources["Barbrush_cj"] as SolidColorBrush;
            //myvalue1.AxisYTitle = "第一个Y轴";
            //myvalue1.AxisYAxisName = myvalue1.AxisName;
            //myvalue1.AxisYAutoRange = false;
            //myvalue1.AxisYMinValue = 0;
            //myvalue1.AxisYMaxValue = 80;
            //myvalue1.AxisYStep = 10;
            MultiYChartValue myvalue2 = new MultiYChartValue();
            myvalue2.LegendLableName = "项目数";
            myvalue2.AxisName = "Secondary";//必须与XAML中对应
            myvalue2.YValueName = "Xms";
            myvalue2.XCategoryName = "Xzqmc";
            //myvalue2.Color = this.Resources["Barbrush_gg"] as SolidColorBrush;
            //myvalue2.AxisYTitle = "第二个Y轴";
            //myvalue2.AxisYAxisName = myvalue2.AxisName;
            //myvalue2.AxisYAutoRange = false;
            //myvalue2.AxisYMinValue = 0;
            //myvalue2.AxisYMaxValue = 8;
            //myvalue2.AxisYStep = 1;
            List<MultiYChartValue> myvalue = new List<MultiYChartValue>();
            myvalue.Add(myvalue1);
            myvalue.Add(myvalue2);


            MultiYChartInfo<gdData> mydata = new MultiYChartInfo<gdData>();
            mydata.DataSource = data;
            mydata.MultiYChartValues = myvalue;

            Binding(mydata);
        }
        public void BarChartData2()
        {
            List<gdData2> data = new List<gdData2>();
            string[] months = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames;
            data.Add(new gdData2("招拍挂", "2012", 223, 16));
            data.Add(new gdData2("协议出让", "2012", 419, 21));
            data.Add(new gdData2("划拨", "2012", 390, 41));


            MultiYChartValue myvalue1 = new MultiYChartValue();
            myvalue1.LegendLableName = "供地总量";
            myvalue1.AxisName = "Frist";//必须与XAML中对应
            myvalue1.YValueName = "Gdzl";
            myvalue1.XCategoryName = "Gdlx";

            MultiYChartValue myvalue2 = new MultiYChartValue();
            myvalue2.LegendLableName = "项目数";
            myvalue2.AxisName = "Secondary";//必须与XAML中对应
            myvalue2.YValueName = "Xms";
            myvalue2.XCategoryName = "Gdlx";

            List<MultiYChartValue> myvalue = new List<MultiYChartValue>();
            myvalue.Add(myvalue1);
            myvalue.Add(myvalue2);


            MultiYChartInfo<gdData2> mydata = new MultiYChartInfo<gdData2>();
            mydata.DataSource = data;
            mydata.MultiYChartValues = myvalue;

            Binding(mydata);
        }
        /// <summary>
        /// barC
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        public void Binding<T>(MultiYChartInfo<T> info)
        {
            this.RadChart1.SeriesMappings.Clear();
            foreach (MultiYChartValue multiYchart in info.MultiYChartValues)
            {
                SeriesMapping mapping = new SeriesMapping();             
                mapping.SeriesDefinition = new BarSeriesDefinition
                {
                    AxisName = multiYchart.AxisName,
                    ShowItemToolTips = true,
                    ShowItemLabels = true,
                    //Appearance = new SeriesAppearanceSettings() { Fill = multiYchart.Color },
                };
                mapping.LegendLabel = multiYchart.LegendLableName;
                mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.YValue, FieldName = multiYchart.YValueName });
                mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.XCategory, FieldName = multiYchart.XCategoryName });
                mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.LegendLabel, FieldName = multiYchart.XCategoryName });
                this.RadChart1.SeriesMappings.Add(mapping);
            }

            this.RadChart1.ItemsSource = info.DataSource;
        }
    }
    public class gdData
    {
        private double _gdzl;//供地总量
        private double _xms;//项目数
        private string _xzqmc;//行政区名称
        private string _nf;//年份

        public gdData(double Gdzl, double Xms, string Xzqmc, string Nf)
        {
            this._gdzl = Gdzl;
            this._xms = Xms;
            this._xzqmc = Xzqmc;
            this._nf = Nf;
        }

        public double Gdzl
        {
            get
            {
                return this._gdzl;
            }
            set
            {
                this._gdzl = value;
            }
        }

        public double Xms
        {
            get
            {
                return this._xms;
            }
            set
            {
                this._xms = value;
            }
        }

        public string Xzqmc
        {
            get
            {
                return this._xzqmc;
            }
            private set
            {
                this._xzqmc = value;
            }
        }

        public string Nf
        {
            get
            {
                return this._nf;
            }
            private set
            {
                this._nf = value;
            }
        }
    }
    public class gdData2
    {
        private string _gdlx;
        private string _nf;
        private double _gdzl;
        private double _xms;
        public gdData2(string Gdlx, string Nf, double Gdzl, double Xms)
        {
            this._gdlx = Gdlx;
            this._nf = Nf;
            this._gdzl = Gdzl;
            this._xms = Xms;
        }
        public string Gdlx
        {
            get
            {
                return this._gdlx;
            }
            private set
            {
                this._gdlx = value;
            }
        }
        public string Nf
        {
            get
            {
                return this._nf;
            }
            private set
            {
                this._nf = value;
            }
        }
        public double Gdzl
        {
            get
            {
                return this._gdzl;
            }
            private set
            {
                this._gdzl = value;
            }
        }
        public double Xms
        {
            get
            {
                return this._xms;
            }
            private set
            {
                this._xms = value;
            }
        }
    }
    public class MultiYChartInfo<T>
    {
        public MultiYChartInfo()
        {
            this.MultiYChartValues = new List<MultiYChartValue>();
        }
        public List<MultiYChartValue> MultiYChartValues { get; set; }

        public List<T> DataSource { get; set; }
    }
    public class MultiYChartValue
    {
        public string LegendLableName { get; set; }
        public string AxisName { get; set; }
        public string YValueName { get; set; }
        public string XCategoryName { get; set; }
        public SolidColorBrush Color { get; set; }

    }
}
